package com.ask.ui;

import com.ask.sms.serverku;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;
import org.smslib.AGateway;
import org.smslib.Service;

public class TrayUI {

    private static boolean ready;

    public static void main(String[] args) throws InterruptedException {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                serverku srvSms = new serverku();
                srvSms.start();
                TrayUI trayUI = new TrayUI();

            }
        });
    }

    public static void setReadyGateway(boolean b) {
        ready = b;
    }
    //Create Pop menu Item
    private final SystemTray tray = SystemTray.getSystemTray();
    private final TrayIcon trayIcon = new TrayIcon(createImage("/com/ask/resources/iliski.png",
            "tray icon"));
    private PopupMenu popup = null;
    private MenuItem aboutItem = null;
    private static MenuItem infoServer = null;
    private Menu statusServer = null;
    private MenuItem startItem = null;
    private MenuItem stopItem = null;
    private MenuItem chatItem = null;
    private MenuItem exitItem = null;
    //jNotif
    //private JNotifWindow JNotif = new JNotifWindow();
    // private static Boolean connect = false;

    public TrayUI() {
        createAndShowGUI();
    }

    private void createAndShowGUI() {
        // Check the SystemTray support
        if (!SystemTray.isSupported()) {
            System.out.println("SystemTray is not supported");
            return;
        }
        popup = new PopupMenu();

        //final SystemTray tray = SystemTray.getSystemTray();
        // Create a popup menu components
        aboutItem = new MenuItem("About");
        infoServer = new MenuItem("Disconnect");
        infoServer.setName("infoserver");
        statusServer = new Menu("Sms");
        startItem = new MenuItem("Start");
        stopItem = new MenuItem("Stop");
        chatItem = new MenuItem("Chat");
        exitItem = new MenuItem("Exit");
        // Add components to popup menu
        popup.add(aboutItem);
        popup.addSeparator();
        popup.add(infoServer);
        popup.addSeparator();
        popup.add(statusServer);

        statusServer.add(startItem);
        statusServer.add(stopItem);
        statusServer.add(chatItem);

        popup.add(exitItem);

        trayIcon.setPopupMenu(popup);
        trayIcon.setImageAutoSize(true);
        trayIcon.setToolTip("iliskiSend ~ www.kriyamuda.com");

        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.out.println("TrayIcon could not be added.");
            return;
        }

        aboutItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JNotifWindow.showNotif("iliskiSend ~ Kriyamuda,PT ");
            }
        });

        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MenuItem item = (MenuItem) e.getSource();
                // TrayIcon.MessageType type = null;
                System.out.println(item.getLabel() + " :: " + item.getName());
                if ("Start".equals(item.getLabel())) {
                    //Start Event 
                    startOnServer();

                } else if ("Chat".equals(item.getLabel())) {

                    System.err.println("--__--?" + ready);
                    if (ready) {
                        infoServer.setLabel("Connect");
                        System.err.println("----?" + ready);
                    }

                } else if ("Stop".equals(item.getLabel())) {
                    stopOnServer();

                } else if ("infoserver".equals(item.getName())) {
                    System.err.println("infoServer");
                    infoserver();
                }
            }
        };

        startItem.addActionListener(listener);
        stopItem.addActionListener(listener);
        infoServer.addActionListener(listener);
        chatItem.addActionListener(listener);

        exitItem.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        tray.remove(trayIcon);
                        System.exit(0);
                    }
                });
    }

    // Obtain the image URL
    protected static Image createImage(String path, String description) {
        URL imageURL = TrayUI.class.getResource(path);

        if (imageURL == null) {
            System.err.println("Resource not found: " + path);
            return null;
        } else {
            return (new ImageIcon(imageURL, description)).getImage();
        }
    }

    private void startOnServer() {
        if (!ready) {
            JNotifWindow.showNotif("Service SMS Belum Siap, Masih Proses ...", true);
        } else {
            Service.ServiceStatus ss = Service.getInstance().getServiceStatus();
            if (ss == Service.ServiceStatus.STOPPED) {
                try {
                    Service.getInstance().startService();
                    setConnect(true);
                } catch (Exception ex) {
                    System.err.println(ex.getMessage());
                }
            } else {
                JNotifWindow.showNotif("Service SMS Sudah Start", true);
            }
        }
    }

    private void stopOnServer() {
        if (!ready) {
            JNotifWindow.showNotif("Service SMS Belum Siap, Masih Proses ...", true);
        } else {
            Service.ServiceStatus ss = Service.getInstance().getServiceStatus();
            if (ss == Service.ServiceStatus.STARTED || ss == Service.ServiceStatus.STARTING) {
                try {
                    Service.getInstance().stopService();
                    setConnect(false);
                } catch (Exception ex) {
                    System.err.println(ex.getMessage());
                }
            } else {
                JNotifWindow.showNotif("Service SMS Sudah Stop", true);
            }
        }
    }

    private void infoserver() {
        if (!ready) {
            JNotifWindow.showNotif("Service SMS Belum Siap, Masih Proses ...", true);
        } else {
            for (org.smslib.AGateway gtw : Service.getInstance().getGateways()) {
                if (gtw.getStatus() == AGateway.GatewayStatuses.STARTED) {
                    JNotifWindow.showNotif(gtw.getGatewayId() + " :--: ", true);
                }
            }
        }
    }

    /**
     * @param aConnect the connect to set
     */
    public static void setConnect(Boolean aConnect) {
        if (aConnect) {
            infoServer.setLabel("Connect");
        } else {
            infoServer.setLabel("Disconnect");
        }
    }
}
